local redis = require "resty.redis"  --依赖包
local red = redis:new()   ---创建客户端
red:set_timeout(6000) -- 1 sec
local ok,err=red.connect(red,'192.168.213.26',6379)  --连接地址和端口
if not ok then
    ngx.say("failed to connect:",err)  ---如果连接错误打印
return
end


ok, err = red:auth("123456")   --输入redis 密码
if not ok then
    ngx.say("failed to auth: ", err)   --密码验证失败
    return
end

ok, err = red:set("dog", "an animal")    --设置key dog
if not ok then
    ngx.say("failed to set dog: ", err)   --
    return
end

ngx.say("set result: ", ok)

local res, err = red:get("dog")   --取出刚才设置的值
if not res then
    ngx.say("failed to get dog: ", err)
    return
end

if res == ngx.null then
    ngx.say("dog not found.")
    return
end

ngx.say("dog: ", res)   ---打印redis 中dog的值

red:init_pipeline()  --批量提交。
red:set("cat", "Marry")   ----
red:set("horse", "Bob")
red:get("cat")
red:get("horse")
local results, err = red:commit_pipeline()
if not results then
    ngx.say("failed to commit the pipelined requests: ", err)
    return
end

for i, res in ipairs(results) do
    if type(res) == "table" then
        if res[1] == false then
            ngx.say("failed to run command ", i, ": ", res[2])
        else
            -- process the table value
        end
    else
        -- process the scalar value
    end
end

-- put it into the connection pool of size 100,
-- with 10 seconds max idle time
local ok, err = red:set_keepalive(10000, 100)
if not ok then
    ngx.say("failed to set keepalive: ", err)
    return
end

-- or just close the connection right away:
-- local ok, err = red:close()
-- if not ok then
--     ngx.say("failed to close: ", err)
--     return
-- end